import tkinter as tk
import random
def click():
    listbox02.delete(0,"end")    #清空文本框
    s=text01.get()
    pos=int(s)
    for i in range(0,7):
        for j in range(7,i,-1):
            if a[j]<a[j-1]:
                a[j-1],a[j]=a[j],a[j-1]
                if pos==j:
                    pos=j-1
                    s=s+"→"+str(pos)
                elif pos==j-1:
                    pos=j
                    s=s+"→"+str(pos)
    for i in range(0,8):
        listbox02.insert(tk.END,a[i])
    #lb05['text'] = s
    #lb05.configure(text = s)
    lb05.config(text = s)    
win = tk.Tk()
win.title('探究冒泡排序')
win.geometry('450x300')
# 阻止Python GUI的大小调整
win.resizable(0, 0)
# 建组件
lb01 = tk.Label(win, text='排序前')
listbox01 = tk.Listbox(win)
lb02 = tk.Label(win, text='初始位置:')
text01 = tk.Entry(win)
lb03 = tk.Label(win, text='排序后')
listbox02 = tk.Listbox(win)
lb04 = tk.Label(win, text='位置变化情况:')
lb05 = tk.Label(win, text='')
a=[]
for i in range(8):
    b = random.randint(0,100)
    listbox01.insert(tk.END,str(b))
    a.append(b)  
btn = tk.Button(win, text='排序', command=click)   
# 布局
lb01.place(x=30, y=15)
listbox01.place(x=15, y=35, width=75, height=200)
lb02.place(x=100, y=60, )
text01.place(x=170, y=60, width=120, height=30)
lb03.place(x=345, y=15, )
listbox02.place(x=330, y=35, width=75, height=200)
lb04.place(x=80, y=250, )
lb05.place(x=160, y=250, )
btn.place(x=140, y=130, width=150, height=40)
# 接收窗体事件
win.mainloop()
